package wsz.email.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * 发送邮件配置
 * @author wsz
 * @date 2021/12/28 19:16
 **/
@Component
public class EmailConfiguration {

    /**
     * 启动类：提供额外的扫描包
     */
    @Autowired
    private MailProperties emailConfig;

    @Bean
    public JavaMailSenderImpl initJavaMailSenderImpl() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setUsername(emailConfig.getUsername());
        javaMailSender.setPassword(emailConfig.getPassword());
        javaMailSender.setHost(emailConfig.getHost());
        javaMailSender.setPort(emailConfig.getPort());
        javaMailSender.setProtocol(emailConfig.getProtocol());
        javaMailSender.setDefaultEncoding(String.valueOf(emailConfig.getDefaultEncoding()));

        Properties mailProperties = new Properties();
        mailProperties.put("mail.smtp.auth", true);
        mailProperties.put("mail.smtp.starttls.enable", true);
        mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        mailProperties.put("mail.debug", true);
        mailProperties.put("mail.smtp.ssl.enable", true);
        javaMailSender.setJavaMailProperties(mailProperties);
        return javaMailSender;
    }
}
